package com.lemon.smartsafetynew.ui.adapter.monitor;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.monitor.MonitorDetailItemVo;
import com.lemon.smartsafetynew.util.common.ColorUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/8.
 */

public class MonitorDetailItemAdapter extends LemonRcvAdapter<MonitorDetailItemVo> {

    private RecyclerTClickListener<MonitorDetailItemVo> mListener;

    public MonitorDetailItemAdapter(@Nullable List<MonitorDetailItemVo> data,
                                    RecyclerTClickListener<MonitorDetailItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new MonitorDetailItem();
    }

    private class MonitorDetailItem implements AdapterItem<MonitorDetailItemVo> {

        private LinearLayout itemDetailLayout;
        private TextView monitorItemName;
        private TextView monitorItemValue;
        private ImageView monitorItemStatusImageView;

        private MonitorDetailItemVo mData;
        private int mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_monitor_detail;
        }

        @Override
        public void bindViews(View root) {

            monitorItemName = (TextView) root.findViewById(R.id.monitor_item_name);
            monitorItemValue = (TextView) root.findViewById(R.id.monitor_item_value);
            monitorItemStatusImageView = (ImageView) root.findViewById(R.id.monitor_item_status_imageView);
            itemDetailLayout = (LinearLayout) root.findViewById(R.id.item_detail_layout);
        }

        @Override
        public void setViews() {
            itemDetailLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if("0".equals(mData.getType())) {  //0为数字型，1为状态型
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
        }

        @Override
        public void handleData(MonitorDetailItemVo monitorDetailItemVo, int position) {

            mData = monitorDetailItemVo;
            mPosition = position;


            if("0".equals(monitorDetailItemVo.getType())) {
                monitorItemName.setText(monitorDetailItemVo.getName());
                monitorItemValue.setText(monitorDetailItemVo.getValue() + " " + monitorDetailItemVo.getUnit());
            } else {
                monitorItemName.setText(monitorDetailItemVo.getName());
                monitorItemValue.setText(monitorDetailItemVo.getStatus());
            }

            if("正常".equals(monitorDetailItemVo.getStatus())) {
                monitorItemValue.setTextColor(Color.parseColor(ColorUtil.monitorDetailItemColor[0]));
                monitorItemStatusImageView.setVisibility(View.INVISIBLE);
            } else if("无响应".equals(monitorDetailItemVo.getStatus())) {
                monitorItemValue.setTextColor(Color.parseColor("#D3D2D0"));
                monitorItemStatusImageView.setVisibility(View.INVISIBLE);
            } else if("未知".equals(monitorDetailItemVo.getStatus())) {
                monitorItemValue.setTextColor(Color.parseColor("#D3D2D0"));
                monitorItemStatusImageView.setVisibility(View.INVISIBLE);
            } else {
                monitorItemValue.setTextColor(Color.parseColor(ColorUtil.monitorDetailItemColor[1]));
                monitorItemStatusImageView.setVisibility(View.INVISIBLE);
            }

        }
    }

}
